using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Entities;
namespace DAL
{
   public class tbChitietdiadanhamthucDAL
    {
       private SqlConnection cn = null;
        public tbChitietdiadanhamthucDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbChitietdiadanhamthuc ctd)
        {
            SqlCommand cm = new SqlCommand("sp_CTdiadanhamthuc_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madiadanh", ctd.Madiadanh);
            cm.Parameters.AddWithValue("@maamthuc", ctd.Maamthuc);
            cm.ExecuteNonQuery();
        }
        public void Update(tbChitietdiadanhamthuc ctd)
        {
            SqlCommand cm = new SqlCommand("sp_CTdiadanhamthuc_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@mactdiadanhamthuc", ctd.Mactdiadanhamthuc);
            cm.Parameters.AddWithValue("@madiadanh", ctd.Madiadanh);
            cm.Parameters.AddWithValue("@maamthuc", ctd.Maamthuc);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("sp_CTdiadanhamthuc_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@mactdiadanhamthuc", iD);
            cm.ExecuteNonQuery();
        }
        public tbCTdiadanhamthucCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_CTdiadanhamthuc_Get_2000", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbCTdiadanhamthucCollection ctddamColl = new tbCTdiadanhamthucCollection();

            while (dr.Read()) 
            {
                tbChitietdiadanhamthuc ctddat = new tbChitietdiadanhamthuc();
                ctddat.Mactdiadanhamthuc = int.Parse(dr.GetValue(0).ToString());
                ctddat.Madiadanh = int.Parse(dr.GetValue(1).ToString());
                ctddat.Maamthuc = int.Parse(dr.GetValue(2).ToString());
                ctddamColl.Add(ctddat);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return ctddamColl;
        }
    }
}
